package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ysd.entity.LunBoTu;
import com.ysd.util.DBManager;

public class LunBoTuDaoimpl {
	/**
	 * 动态获取轮播人的数据分页
	 * @return
	 */
	public ArrayList<LunBoTu> getLunBoTu(int page,int size ){
		ArrayList<LunBoTu> list=new ArrayList<LunBoTu>();
		Connection conn=DBManager.getConnection();
		
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			String sql="SELECT * FROM lunbotu limit "+(page-1)*size+","+size;
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				LunBoTu  lbt=new LunBoTu(rs.getInt("fenleiId"), rs.getString("lunbotu"));
				list.add(lbt);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 动态获取轮播人的数据
	 * @return
	 */
	public ArrayList<LunBoTu> getLunBoTu(){
		ArrayList<LunBoTu> list=new ArrayList<LunBoTu>();
		Connection conn=DBManager.getConnection();
		
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			String sql="SELECT * FROM lunbotu ";
			 rs=st.executeQuery(sql);
			while(rs.next()) {
				LunBoTu  lbt=new LunBoTu(rs.getInt("fenleiId"), rs.getString("lunbotu"));
				list.add(lbt);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	public ArrayList<LunBoTu> getLunBoTuSize(){
	ArrayList<LunBoTu> list=new ArrayList<LunBoTu>();
	Connection conn=DBManager.getConnection();
	
	Statement st=null;
	ResultSet rs=null;
	try {
		 st=conn.createStatement();
		String sql="SELECT * FROM lunbotu ";
		 rs=st.executeQuery(sql);
		while(rs.next()) {
			LunBoTu  lbt=new LunBoTu(rs.getInt("fenleiId"), rs.getString("lunbotu"));
			list.add(lbt);
		}
		return list;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}finally {
		DBManager.closeAll(conn, st, rs);
	}
}
	/**
	 * 根据id删除轮播图
	 * @param id
	 * @return
	 */
	public int delLunBoTu(int id) {
		return DBManager.executeUpdate("delete from lunbotu where fenleiId="+id);
	}
	
	
	/**
	 * 添加轮播图
	 * @param l
	 * @return
	 */
	public int addLunBoTu(LunBoTu l) {
		return DBManager.executeUpdate("INSERT into lunbotu VALUES(null,'"+l.getLunbotu()+"')");
	}
	
	
	
	/**
	 * 根据轮播图name查询数据
	 * @param l
	 * @return
	 */
	public ArrayList<LunBoTu> selLunBoTuByName(String name){
		ArrayList<LunBoTu> list=new ArrayList<LunBoTu>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			 st=conn.createStatement();
			
			 rs=st.executeQuery("SELECT * from lunbotu where lunbotu='"+name+"'");
			while(rs.next()) {
				int fenleiId=rs.getInt("fenleiId");
				String lunbotu=rs.getString("lunbotu");
				LunBoTu lbt=new LunBoTu(fenleiId, lunbotu);  
				list.add(lbt);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	public LunBoTu selLunBoTuById(int id){
		
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		LunBoTu lbt=null;
		try {
			 st=conn.createStatement();
			
			 rs=st.executeQuery("SELECT * FROM lunbotu WHERE fenleiId="+id);
			while(rs.next()) {
				int fenleiId=rs.getInt("fenleiId");
				String lunbotu=rs.getString("lunbotu");
				 lbt=new LunBoTu(fenleiId, lunbotu);  
				
			}
			return lbt;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	
	public int editlunBoTuById(LunBoTu l) {
		return DBManager.executeUpdate("UPDATE lunbotu set lunbotu = '"+l.getLunbotu()+"' WHERE fenleiid= "+l.getFenleiId());
	}
}
